package com.shankungfu.adapter;

import java.util.Collections;
import java.util.LinkedList;
import java.util.List;

import android.content.Context;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.display.FadeInBitmapDisplayer;
import com.nostra13.universalimageloader.core.listener.ImageLoadingListener;
import com.nostra13.universalimageloader.core.listener.SimpleImageLoadingListener;
import com.shankungfu.R;
import com.shankungfu.bean.CharityNewsDataBean;

public class PublicInformationAdapter extends BaseAdapter {
	private LayoutInflater mInflater;
	private Context context;
	private List<CharityNewsDataBean> charityNewsData;
	private DisplayImageOptions options;
	private ImageLoadingListener animateFirstListener = new AnimateFirstDisplayListener();
	private ImageLoader imageLoader;

	public PublicInformationAdapter(Context context, DisplayImageOptions options) {
		this.context = context;
		this.options = options;
		imageLoader = ImageLoader.getInstance();
		mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	}
	
	public void setData(List<CharityNewsDataBean> charityNewsData) {
		this.charityNewsData = charityNewsData;
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return charityNewsData != null ? charityNewsData.size() : 0;
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return charityNewsData.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup arg2) {
		CharityNewsDataBean charityNews = charityNewsData.get(position);
		ViewHolder holder = null;
		if (convertView == null) {
			holder = new ViewHolder();
			convertView = mInflater.inflate(R.layout.public_information_item_layout,
					null);
			holder.title = (TextView) convertView.findViewById(R.id.report_title);
			holder.description = (TextView) convertView.findViewById(R.id.report_des);
			holder.imgView = (ImageView) convertView.findViewById(R.id.public_image);

			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}

		holder.title.setText(charityNews.getCharity_news_title());
		holder.description.setText(charityNews.getCharity_news_title());
		imageLoader.displayImage(charityNews.getCharity_news_img_url(), holder.imgView, options, animateFirstListener);

		return convertView;
	}

	private class ViewHolder {
		private TextView title;
		private TextView description;
		private ImageView imgView;
	}
	
	private static class AnimateFirstDisplayListener extends SimpleImageLoadingListener {

		static final List<String> displayedImages = Collections.synchronizedList(new LinkedList<String>());

		@Override
		public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
			if (loadedImage != null) {
				ImageView imageView = (ImageView) view;
				boolean firstDisplay = !displayedImages.contains(imageUri);
				if (firstDisplay) {
					FadeInBitmapDisplayer.animate(imageView, 500);
					displayedImages.add(imageUri);
				}
			}
		}
	}

}
